﻿//// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
//// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
//// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
//// PARTICULAR PURPOSE.
////
//// Copyright (c) Microsoft Corporation. All rights reserved

/// <reference path="//Microsoft.WinJS.0.6/js/base.js" />
/// <reference path="//Microsoft.WinJS.0.6/js/ui.js" />

(function () {

    function clearSampleOptions() {
        var selectElement = document.querySelector("#scenarios");
        WinJS.Utilities.empty(selectElement);
    }

    function clearSampleDetails() {
        var detailsDiv = document.querySelector(".details");
        WinJS.Utilities.query(".item", detailsDiv).forEach(
            function (e) {
                detailsDiv.removeChild(e);
            }
        );
    }

    function clearSampleOutputs() {
        var outputDiv = document.querySelector("#output");
        WinJS.Utilities.query(".item", outputDiv).forEach(
            function (e) {
                outputDiv.removeChild(e);
            }
        );
    }

    function getScenarioText(scenarioFragment) {
        var scenarioSpan = scenarioFragment.querySelector("#scenarioTitle");
        return scenarioSpan.innerText;
    }

    function loadScenarioFragment(scenarioName) {
        var uri = "scenarios/" + scenarioName;
        return WinJS.UI.Fragments.render(uri);
    }

    function addScenarioOption(selectElement, optionText, optionIndex) {
        var scenarioNum = optionIndex + 1;
        var optionElement = document.createElement("option");
        if (optionIndex === 0) {
            optionElement.selected = "selected";
        }
        optionElement.text = scenarioNum + ") " + optionText;
        optionElement.value = scenarioNum;
        selectElement.appendChild(optionElement);
    }

    function addScenarioDetails(parentDiv, fragment, optionIndex) {
        var scenarioNum = optionIndex + 1;
        var detailsDiv = fragment.querySelector("#scenarioInput");
        detailsDiv.id = "scenario" + scenarioNum + "Input";
        parentDiv.appendChild(detailsDiv);
    }

    function addScenarioOutputs(parentDiv, fragment, optionIndex) {
        var scenarioNum = optionIndex + 1;
        var outputDiv = fragment.querySelector("#scenarioOutput");
        outputDiv.id = "scenario" + scenarioNum + "Output";
        parentDiv.appendChild(outputDiv);
    }

    function runAfterAttachScript(fragment) {
        var markerElement = fragment.querySelector("[data-win-fragmentadded]");
        if (markerElement) {
            var initMethodName = markerElement.getAttribute("data-win-fragmentadded");
            var initMethod = WinJS.Utilities.getMember(initMethodName);
            if (initMethod) {
                initMethod();
            }
        }
    }

    function initialize(scenarios) {
        clearSampleOptions();
        clearSampleDetails();
        clearSampleOutputs();

        var scenarioSelect = document.querySelector("#scenarios");
        var detailsDiv = document.querySelector(".details");
        var outputsDiv = document.querySelector("#output");

        var promises = [], fragments = [];

        for (var i = 0, len = scenarios.length; i < len; i++) {
            promises.push(
                loadScenarioFragment(scenarios[i]).then((function (fragIndex) {
                    return function (frag) {
                        fragments[fragIndex] = frag;
                    };
                }
            )(i)));
        }

        return WinJS.Promise.join(promises).then(function () {
            var fragsLength = fragments.length;
            for (var fragIndex = 0; fragIndex < fragsLength; fragIndex++) {
                var frag = fragments[fragIndex];
                addScenarioOption(scenarioSelect, getScenarioText(frag), fragIndex);
                addScenarioDetails(detailsDiv, frag, fragIndex);
                addScenarioOutputs(outputsDiv, frag, fragIndex);
                runAfterAttachScript(frag);
            }
            sdkSample.selectScenario(1);
        });
    }

    WinJS.Namespace.define("sdkSample", {
        initializeScenarios: initialize
    });
})();
